Introduction

Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.

print("Hello, World!")

Syntax

Python Variables

In Python, variables are created when you assign a value to it:

x = 5 y = "Hello, World!"

Python Comments

Python has commenting capability for the purpose of in-code documentation. Comments start with a #, and Python will render the rest of the line as a comment:

#This is a comment. print("Hello, World!")
Data types

In programming, data type is an important concept. Variables can store data of different types, and different types can do different things. Python has the following data types built-in by default, in these categories:

x = 5 print(type(x))

Used to get the data type

Conditional statement

Python Conditions and If statements

Python supports the usual logical conditions from mathematics:

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <=b
  • Greater than: a> b
  • Greater than or equal to: a >= b
  • IF statement

    An "if statement" is written by using the if keyword.

    aa = 33 b = 200 if b > a: print("b is greater than a")

    Indentation

    Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.

    a = 33 b = 200 if b > a: print("b is greater than a") # you will get an error

    ELIF statement

    The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition".

    a = 33 b = 33 if b > a: print("b is greater than a") elif a == b: print("a and b are equal")
    for loop

    ELIF statement

    A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages. With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.

    fruits = ["apple", "banana", "cherry"] for x in fruits: print(x)

    The break Statement

    With the break statement we can stop the loop before it has looped through all the items:

    fruits = ["apple", "banana", "cherry"] for x in fruits: print(x) if x == "banana": break
    Python Functions

    A function is a block of code which only runs when it is called You can pass data, known as parameters, into a function. A function can return data as a result.

    Creating a Function

    In Python a function is defined using the def keyword:

    def my_function(): print("Hello from a function")

    Calling a Function

    To call a function, use the function name followed by parenthesis:

    def my_function(): print("Hello from a function") my_function())
    File handling

    File handling is an important part of any web application. Python has several functions for creating, reading, updating, and deleting files.

    f = open("demofile.txt")

    To open a file for reading it is enough to specify the name of the file: